package com.example.minademo.mina;

import org.apache.mina.core.service.IoHandlerAdapter;
import org.apache.mina.core.session.IdleStatus;
import org.apache.mina.core.session.IoSession;

import java.util.Date;

public class MinaServiceHandler extends IoHandlerAdapter {
    @Override
    public void sessionCreated(IoSession ioSession) throws Exception {
        super.sessionCreated(ioSession);
        System.out.println("sessionId = " + ioSession.getId());
        MinaServiceManager.getInstance().addSession(ioSession);
    }

    @Override
    public void sessionOpened(IoSession ioSession) throws Exception {
        super.sessionOpened(ioSession);
    }

    @Override
    public void sessionClosed(IoSession ioSession) throws Exception {
        super.sessionClosed(ioSession);
    }

    @Override
    public void sessionIdle(IoSession ioSession, IdleStatus idleStatus) throws Exception {
        super.sessionIdle(ioSession,idleStatus);
    }

    @Override
    public void exceptionCaught(IoSession ioSession, Throwable throwable) throws Exception {
        super.exceptionCaught(ioSession,throwable);
    }

    @Override
    public void messageReceived(IoSession ioSession, Object o) throws Exception {
        super.messageReceived(ioSession,o);
        String message = o.toString();
        Date date = new Date();
        ioSession.write(date.toString());
        System.out.println(message);
    }

    @Override
    public void messageSent(IoSession ioSession, Object o) throws Exception {
        super.messageSent(ioSession,o);
    }

    @Override
    public void inputClosed(IoSession ioSession) throws Exception {
        super.inputClosed(ioSession);
    }
}
